package com.example.controller;

import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 去重书目数据
     * @param originalPath
     * @return 字符串提示信息
     */
    @PutMapping("/deduplicate")
    public String deduplicateBook(String originalPath){
        return bookService.deduplicateBook(originalPath);
    }

    /**
     * 按照中图分类前缀填充标签
     * @param originalPath
     * @param targetPath
     * @return 字符串提示信息
     */
    @PutMapping("/fill/label")
    public String fillLabelName(String originalPath, String targetPath){
        return bookService.fillLabelName(originalPath,targetPath);
    }

    /**
     * 合并CSV文件数据
     * @param dirPath 目录路径
     * @return 字符串提示信息
     */
    @PutMapping("/merge/csv")
    public String mergeCSV(String dirPath){
        return bookService.mergeCSV(dirPath);
    }

    /**
     * 合并xlsx文件数据
     * @param dirPath 目录路径
     * @return 字符串提示信息
     */
    @PutMapping("/merge/xlsx")
    public String mergeXlsx(String dirPath){
        return bookService.mergeXlsx(dirPath);
    }

    /**
     * 清除服务器不存在的图片
     * @param originalPath
     * @return 字符串提示信息
     */
    @PutMapping("/image/exist")
    public String checkImageExist(String originalPath){
        return bookService.checkImageExist(originalPath);
    }

    /**
     * 清除多个文件中服务器不存在的图片
     * @param originalPathList
     * @return 字符串提示信息
     */
    @PutMapping("/multi/image/exist")
    public String multiCheckImageExist(@RequestParam("originalPathList") List<String> originalPathList){
        if(originalPathList==null){
            return "文件不存在";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String originalPath : originalPathList) {
            String s = bookService.checkImageExist(originalPath);
            stringBuilder.append(originalPath.substring(originalPath.lastIndexOf('/')+1)+s+"\n");
            System.out.println("----------------------------------------"+originalPath.substring(originalPath.lastIndexOf('/')+1)+s+"-------------------------------------------------------");
        }
        return stringBuilder.toString();
    }

    /**
     * 添加保值率
     * @param originalPath
     * @return 字符串提示信息
     */
    @PutMapping("/add/preservation")
    public String addPreservation(String originalPath) {
        return bookService.addPreservation(originalPath);
    }

    /**
     * 提取csv文件到一个文件夹
     */
    @PutMapping("/extract/csv")
    public String extractCSV(String originalDirPath,String targetDirPath) throws IOException {
        return bookService.extractCSV(originalDirPath,targetDirPath);
    }

    /**
     * 根据isbn和价格、其它内容有无一致 去重
     */
    @PutMapping("/deduplicate/isbnPriceOther")
    public String deduplicateByIsbnPriceOther(String originalPath){
        return bookService.deduplicateByIsbnPriceOther(originalPath);
    }

}
